package com.sch.www.ele.services.impl;


import com.sch.www.codeGenerator.base.BaseServicesSimpleImp;
import com.sch.www.codeGenerator.base.CustomPage;
import com.sch.www.ele.entity.CareGroup;
import com.sch.www.ele.entity.CareGroupExample;
import com.sch.www.ele.entity.CareUser;
import com.sch.www.ele.mapper.CareGroupMapper;
import com.sch.www.ele.services.CareGroupServices;
import com.sch.www.ele.services.CareMemberServices;
import com.sch.www.ele.services.CareUnitServices;
import com.sch.www.ele.services.CareUserServices;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CareGroupServicesImpl extends BaseServicesSimpleImp<CareGroup, CareGroupExample, CareGroupMapper, CareGroupServices> implements CareGroupServices {

    @Autowired
    CareUserServices careUserServices;

    @Autowired
    CareUnitServices careUnitServices;

    @Autowired
    CareMemberServices careMemberServices;

    @Override
    protected CareGroupExample getExampleNewInstance() {
        return new CareGroupExample();
    }

    @Override
    protected String getTableName() {
        return null;
    }


    @Override
    public CustomPage<CareGroup> list(String name, String regionCode, int pageNum, int pageSize) {
        CareGroupExample example = getExampleNewInstance();
        CareGroupExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(name)) {
            criteria.andNameLike(likePacking(name));
        }
        if (StringUtils.isNotBlank(regionCode)) {
            criteria.andRegionCodeLike(regionCode + "%");
        }
        return getMapper().selectByExampleStartPage(example, pageNum, pageSize);
    }


    @Override
    public int insertSelective(String name, String userId) {
        CareUser user = careUserServices.selectById(userId);
        CareGroup group = new CareGroup();
        group.setName(name);
        group.setCareUnitId(user.getCareUnitId());
        group.setLeaderUserId(user.getId());
        group.setLeaderUserName(user.getName());
        return insertSelective(group);
    }

    @Override
    public boolean updateByIdSelective(String id, String name, String userId) {
        careUnitServices.insertSelective();
        CareUser user = careUserServices.selectById(userId);
        CareGroup group = new CareGroup();
        group.setId(id);
        group.setName(name);
        group.setCareUnitId(user.getCareUnitId());
        group.setLeaderUserId(user.getId());
        group.setLeaderUserName(user.getName());
        return updateByIdSelective(group);
    }
}
